<template>
  <div class='container'>
     <div class="app-container">
      <el-card shadow="never">
        <!-- 面包屑导航 -->
      <div  slot="header" v-if="subject.id && subject.name">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{subject.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
        <!-- 搜索 -->
        <el-form ref="ruleForm" :inline="true" :model="form">
            <el-row :gutter="20">
              <el-col :span="6">
                <el-form-item label="标签名称">
                  <el-input
                    style="width: 180px;"
                    placeholder="请输入"
                    class="filter-item"
                    v-model="form.tagName"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="状态">
                  <el-select class="filter-item" style="width: 190px;" v-model="form.state" >
                    <el-option label="已启用" value="1"></el-option>
                    <el-option label="已禁用" value="0"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                  <el-form-item>
                    <el-button @click="resetForm">清除</el-button>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="getList">搜索</el-button>
                  </el-form-item>
              </el-col>
              <el-col :span="5" :offset="4">
                <el-button type="text" icon="el-icon-back" v-if="subject.id && subject.name" size="small" @click="$router.back()">返回学科</el-button>
                <el-button
                  size="small"
                  @click="handleChange('add')"
                  type="success"
                  icon="el-icon-edit"
                >新增标签</el-button>
              </el-col>
            </el-row>

        </el-form>
        <!-- 数据记录 -->
        <el-alert type="info" class="alert" :closable="false" show-icon >
          <template #title>
            数据共{{total}}条
          </template>
          </el-alert>
        <!-- 数据列表 -->
        <el-table
          :data="tableData"
          element-loading-text="给我一点时间"
          fit
          highlight-current-row
          style="width: 100%"
        >
          <el-table-column align="center" label="序号" type="index" :index="indexMethod"></el-table-column>
          <el-table-column align="center" label="所属学科" prop="subjectName"></el-table-column>
          <el-table-column align="center" label="标签名称" prop="tagName"></el-table-column>
          <el-table-column label="创建者" prop="username"></el-table-column>
          <el-table-column align="center" label="创建日期">
            <template #default="{row}">
              {{row.addDate | parseTimeByString}}
            </template>
          </el-table-column>

          <el-table-column align="center" label="状态">
            <template #default="{row}">
            {{row.state===1 ? '已启用' : '已禁用'}}
            </template>
          </el-table-column>

          <!-- 操作按钮 -->
          <el-table-column
            align="center"
            label="操作"
            width="280"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button type="text"  @click="handleStatus(scope.row)">
                <span v-if="scope.row.state==0">启用</span>
                <span v-else>禁用</span>
              </el-button>
              <el-button type="text" :disabled="scope.row.state == 1" @click="handleChange('edit',scope.row)">修改</el-button>
              <el-button
                :disabled="scope.row.state == 1"
                type="text"
                @click="delArticle(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- end -->
        <!-- 分页 -->
        <div class="pagination">
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page"
              :total="total"
              :page-size="pagesize"
              :page-sizes="[5,10,20,30, 50]"
              layout=" prev, pager, next,sizes, jumper"
            ></el-pagination>

        </div>
      </el-card>
    </div>
    <!-- 新增或修改文章 -->
    <TagsAdd :addArticleDialog.sync="addArticleDialog" ref="addArticles"  @uploadData="getList" :subject="subject"></TagsAdd>
  </div>
</template>

<script>
import { list, changeState, detail, remove } from '@/api/hmmm/tags.js'
import TagsAdd from '../components/tags-add.vue'
export default {
  components: { TagsAdd },
  name: 'tags',
  data () {
    return {
      form: {
        tagName: '',
        state: null
      },
      tableData: [],
      page: 1,
      pagesize: 10,
      total: 10,
      videoUrl: null,
      addArticleDialog: false,
      subjectID: null,
      subject: {} // 从学科管理调过来带的query参数，有id和name
    }
  },
  created () {
    console.log(this.$route.query) // 如果是学科管理跳过来的，query或者params不是空的
    this.subject = this.$route.query
    this.getList()
  },
  methods: {
    // 处理序列号
    indexMethod (index) {
      return index + (this.page - 1) * this.pagesize + 1
    },
    // 清除按钮
    resetForm () {
      this.form.tagName = ''
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      this.getList()
      // console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getList()
      // console.log(`当前页: ${val}`)
    },
    // 发送请求获取列表
    async getList () {
      const reqData = {
        page: this.page,
        pagesize: this.pagesize,
        tagName: this.form.tagName,
        state: +this.form.state,
        subjectID: this.subject.id
      }
      const { data } = await list(reqData)
      console.log(data)
      this.total = data.counts
      this.tableData = data.items
    },
    // 修改启用禁用状态
    async handleStatus (row) {
      const data = {
        id: row.id,
        state: row.state === 1 ? 0 : 1
      }
      const res = await changeState(data)
      if (res.status === 200) {
        this.$message.success('操作成功')
      }
      this.getList()
    },
    // 新增或修改文章
    async handleChange (btnName, data = {}) {
      this.addArticleDialog = true
      if (btnName === 'add') {
        this.$refs.addArticles.dialogTitle = '新增标签'
      } else {
        this.$refs.addArticles.dialogTitle = '修改标签'
        console.log(data)
        const res = await detail(data)
        console.log(res)
        this.$refs.addArticles.formData = res.data
      }
    },
    // 删除文章
    delArticle (id) {
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        const res = await remove({ id })
        console.log(res)
        if (res.status === 200) {
          this.getList()
          this.$message.success('删除成功!')
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-table th {
  background-color: #fafafa;
}
.el-table th.is-leaf {
  border-bottom: 2px solid #e8e8e8;
}
.disabled td {
  background-color: #f9f9f9;
  color: #c1c1c1;
}
.disabled .el-button--primary,
.disabled .el-button--danger {
  background-color: #dbdada;
  border: 1px solid #dbdada;
  color: #999;
  cursor: not-allowed;
}
.pagination{
  display: flex;
  justify-content: end;
}
.el-icon-film{
  color: blue;
}
.video-preview{
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.3);
  overflow: hidden;
  z-index: 9999;
  .close{
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%,0);
    background: rgba(0,0,0,0.5);
    box-shadow: 0 0 5px rgba(0,0,0,0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .box{
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    video{
      width: 100%;
      height: 100%;
    }
  }
}
</style>
